import numpy as np
import cv2 as cv

file = "../../res/121.jpg"
img = cv.resize(cv.imread(file),None, fx=0.5, fy=0.5)
img = cv.cvtColor(img, cv.COLOR_BGR2GRAY)  # 转为灰度图

thresh = 100
maxval = 255
threshold_type = cv.THRESH_BINARY
ret, thresh_100 = cv.threshold(img, thresh, maxval, threshold_type)
ret, thresh_50 = cv.threshold(img, 50, maxval, threshold_type)
ret, thresh_150 = cv.threshold(img, 150, maxval, threshold_type)
ret, thresh_200 = cv.threshold(img, 200, maxval, threshold_type)

cv.imshow("img", img)
cv.imshow("thresh_50", thresh_50)
cv.imshow("thresh_100", thresh_100)
cv.imshow("thresh_150", thresh_150)
cv.imshow("thresh_200", thresh_200)


help_str = """
w: thresh += 10
s: thresh -= 10

1 : THRESH_BINARY
2: THRESH_BINARY_INV
3: THRESH_TRUNC
4: THRESH_TOZERO
5: THRESH_TOZERO_INV

q: exit
"""
print(help_str)
while True:
    ret, thresh_img = cv.threshold(img, thresh, maxval, threshold_type)
    cv.imshow("thresh_img", thresh_img)
    key = cv.waitKey(1)
    if key == ord("q"):
        break
    elif key == ord("w"):
        thresh += 5
        thresh = min(thresh, 255)
    elif key == ord("s"):
        thresh -= 5
        thresh = max(thresh, 0)


cv.destroyAllWindows()
